package org.csu.demopetstore.Controller;

import org.csu.demopetstore.Service.AccountService;
import org.csu.demopetstore.Service.CartService;
import org.csu.demopetstore.Service.CategortService;
import org.csu.demopetstore.Service.OrderService;
import org.csu.demopetstore.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private CartService cartService;
    @Autowired
    private CategortService categortService;

    @RequestMapping("/getNewOrder")
    public String getNewOrder(Model model, HttpSession session){
        Account account = (Account) session.getAttribute("account");
        model.addAttribute(account);

        Date date = new Date(System.currentTimeMillis());
        String username = account.getUsername();
        Order order = new Order();
        Account accountBill = accountService.getAccountInformation("j2ee");

        int count = orderService.getCount();
        order.setOrderId(count+1);
        order.setUsername(username);
        order.setBillAddress1(accountBill.getAddress1());
        order.setBillAddress2(accountBill.getAddress2());
        order.setBillCity(accountBill.getCity());
        order.setBillCountry(accountBill.getCountry());
        order.setBillState(accountBill.getState());
        order.setBillToFirstName(accountBill.getFirstName());
        order.setBillToLastName(accountBill.getLastName());
        order.setBillZip(accountBill.getZip());
        order.setOrderDate(date);
        order.setShipAddress1(account.getAddress1());
        order.setShipAddress2(account.getAddress2());
        order.setShipCity(account.getCity());
        order.setShipCountry(account.getCountry());
        order.setShipState(account.getState());
        order.setShipToFirstName(account.getFirstName());
        order.setShipToLastName(account.getLastName());
        order.setShipZip(account.getZip());
        order.setTotalPrice((BigDecimal)session.getAttribute("subTotal"));

        List<Cart> cartList = cartService.getCartList(account.getUsername());
        List<LineItem> items = new ArrayList<>();
        int j = 1;
        for (int i=0; i<cartList.size(); i++){
            Cart cart = cartList.get(i);
            Item item = categortService.getItem(cart.getItemid());
            LineItem lineItem = new LineItem();
            lineItem.setOrderId(order.getOrderId());
            lineItem.setLineNumber(j++);
            lineItem.setItemId(cart.getItemid());
            lineItem.setQuantity(cart.getQuantity());
            lineItem.setUnitPrice(cart.getListprice());
            lineItem.setItem(item);
            BigDecimal total = (cart.getListprice()).multiply(BigDecimal.valueOf(cart.getQuantity()));
            lineItem.setTotal(total);

            orderService.insertLineItem(lineItem);
            items.add(lineItem);
        }
        order.setLineItems(items);

        //订单插入
        orderService.insertOrder(order);
        orderService.insertOrderStatus(order);
        model.addAttribute(order);
        session.setAttribute("order",order);
        return "order/together";
    }

    @RequestMapping("/ConfirmOrderForm")
    public String confirmOrderForm(Model model, HttpSession session){
        Account account = (Account) session.getAttribute("account");
        model.addAttribute(account);

        Order order = (Order)session.getAttribute("order");
        model.addAttribute(order);
        return "order/ConfirmOrder";
    }

    @RequestMapping("/viewOrder")
    public String viewOrder(Model model, HttpSession session){
        Account account = (Account) session.getAttribute("account");
        model.addAttribute(account);
        Order order = (Order)session.getAttribute("order");
        model.addAttribute(order);
        cartService.deleteAllCart(account.getUsername());
        return "order/ViewOrder";
    }

    @RequestMapping("/viewOrderDetail")
    public String viewOrderDetail(@RequestParam("orderId") String orderId, Model model, HttpSession session){
        Account account = (Account) session.getAttribute("account");
        model.addAttribute(account);

        Order order = orderService.getOrderByID(Integer.parseInt(orderId));
        List<LineItem> lineItemList = orderService.getLineItem(Integer.parseInt(orderId));
        for (int i=0; i<lineItemList.size(); i++){
            Item item = categortService.getItem(lineItemList.get(i).getItemId());
            BigDecimal total = (lineItemList.get(i).getUnitPrice()).multiply(BigDecimal.valueOf(lineItemList.get(i).getQuantity()));
            lineItemList.get(i).setItem(item);
            lineItemList.get(i).setTotal(total);
        }
        order.setLineItems(lineItemList);
        model.addAttribute(order);
        return "order/ViewOrder";
    }

    @RequestMapping("/ViewListOrder")
    public String ViewListOrder(Model model, HttpSession session){
        Account account = (Account) session.getAttribute("account");
        model.addAttribute(account);
        List<Order> orderList = orderService.getListOrder(account.getUsername());
        model.addAttribute(orderList);
        return "order/ListOrder";
    }
}
